import copy

class Solution(object):
    def pathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: List[List[int]]
        """
        ret = []
        acc = []
        def traverse(root, ac):
            acc.append(root.val)
            ac += root.val
            if (not root.left) and (not root.right) and ac==sum:
                 ret.append(copy.copy(acc))
            if root.left: traverse(root.left, ac)
            if root.right: traverse(root.right, ac)
            acc.pop()
        if root: traverse(root, 0)
        return ret